package com.yiqi.liebang.feature.setting.view;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.RegisterBo;
import com.yiqi.liebang.feature.setting.contract.EditContract;
import com.yiqi.liebang.feature.setting.dependencies.edit.DaggerEditComponent;
import com.yiqi.liebang.feature.setting.dependencies.edit.EditPresenterModule;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

import static com.suozhang.framework.utils.AppUtil.fullScreen;

public class AuthNewPhoneActivity extends BaseActivity implements EditContract.View {

    @Inject
    EditContract.Presenter mPresenter;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_edit_phone_old_phone)
    EditText mEdtEditPhoneOldPhone;
    @BindView(R.id.btn_edit_phone_old_code)
    TextView mBtnEditPhoneOldCode;
    @BindView(R.id.edt_edit_phone_old_code)
    EditText mEdtEditPhoneOldCode;


    private String oldPhone;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_edit_new_phone;
    }

    @Override
    protected void initInjector() {
        DaggerEditComponent.builder().editPresenterModule(new EditPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "设置新手机号", true, true);
        oldPhone = this.getIntent().getStringExtra("old_phone");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initSystemParams() {
//        super.initSystemParams();
        fullScreen(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }


    @OnClick({R.id.btn_edit_phone_old_code, R.id.btn_pwd_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_edit_phone_old_code:
                mPresenter.sendCode(mEdtEditPhoneOldPhone.getText().toString());
                break;
            case R.id.btn_pwd_commit:
                mPresenter.editPhone(
                        oldPhone,
                        mEdtEditPhoneOldCode.getText().toString(),
                        mEdtEditPhoneOldPhone.getText().toString()
                );

                break;
        }
    }

    public void onGetCode() {
        final int count = 60;//倒计时10秒
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//ui线程中进行控件更新
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        mBtnEditPhoneOldCode.setEnabled(false);
                        mBtnEditPhoneOldCode.setTextColor(AM.res().getColor(R.color.primary_comparison));
                    }
                }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long num) {

                mBtnEditPhoneOldCode.setText(num + "s");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                //回复原来初始状态
                mBtnEditPhoneOldCode.setEnabled(true);
                mBtnEditPhoneOldCode.setText("获取验证码");
                mBtnEditPhoneOldCode.setTextColor(AM.res().getColor(R.color.primary_comparison));
            }
        });

    }


    @Override
    public void showCodeSuccess(RegisterBo s) {
        onGetCode();
        mEdtEditPhoneOldCode.setText(s.getCode());
    }

    @Override
    public void showSuccess() {
        showMsg("修改手机号成功！");
        finish();
    }


    @Override
    public void showError(String error) {
        showMsg(error);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
